﻿using Business;
using Entity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BookManageSys.User
{
    public partial class UserInfo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PnlPassword.Visible = false;
                PnlOthers.Visible = false;
            }
        }

        protected void BtnPassword_Click(object sender, EventArgs e)
        {
            try
            {
                PnlOthers.Visible = false;
                PnlPassword.Visible = true;

                TxtPassUserName.Text = Session["UserName"].ToString();
            }
            catch
            {
                Response.Write("<script>alert('请先登录！'),location.href='..//UserLogin.aspx'</script>");
            }
        }

        protected void BtnInfo_Click(object sender, EventArgs e)
        {
            try
            {
                PnlOthers.Visible = true;
                PnlPassword.Visible = false;

                UsersEntity ue = new UsersEntity();
                ue.Username = Session["UserName"].ToString();
                TxtInfoUserName.Text = Session["UserName"].ToString();

                UsersBusiness ub = new UsersBusiness();
                DataTable dt = ub.GetUserInfo(ue);

                TxtInfoUserName.Text = dt.Rows[0]["username"].ToString();
                TxtAddress.Text = dt.Rows[0]["address"].ToString();
                TxtTelephone.Text = dt.Rows[0]["telephone"].ToString();
            }
            catch
            {
                Response.Write("<script>alert('请先登录！'),location.href='..//UserLogin.aspx'</script>");
            }
        }

        protected void BtnPassReset_Click(object sender, EventArgs e)
        {
            UsersEntity ue = new UsersEntity();
            ue.Username = TxtPassUserName.Text;

            UsersBusiness ub = new UsersBusiness();
            object password = ub.GetPasswordByUserName(ue);
            if (TxtPass.Text == password.ToString())
            {
                ue.Password = TxtNewPass.Text;
                int res = ub.ChangeUserPassword(ue);
                if (res > 0)
                {
                    Response.Write("<script>alert('修改成功！'),location.href='UserInfo.aspx'</script>");
                    PnlPassword.Visible = false;

                }
                else
                {
                    Response.Write("<script>alert('修改失败！')</script>");
                }
            }
            else
            {
                Response.Write("<script>alert('原密码错误！')</script>");
            }
        }

        protected void BtnPassCancel_Click(object sender, EventArgs e)
        {
            PnlPassword.Visible = false;
        }

        protected void BtnInfoReset_Click(object sender, EventArgs e)
        {
            UsersEntity ue = new UsersEntity();
            ue.Username = TxtPassUserName.Text;
            ue.Address = TxtAddress.Text;
            ue.Telephone = TxtTelephone.Text;

            UsersBusiness ub = new UsersBusiness();
            int res = ub.ChangeUserInfo(ue);
            if (res > 0)
            {
                Response.Write("<script>alert('修改成功！'),location.href='UserInfo.aspx'</script>");
                PnlOthers.Visible = false;
            }
            else
            {
                Response.Write("<script>alert('修改失败！')</script>");
            }
        }

        protected void BtnInfoCancel_Click(object sender, EventArgs e)
        {
            PnlOthers.Visible = false;
        }
    }
}